package chenjiuxu.seniorui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import chenjiuxu.seniorui.R;

/**
 * Created by 15705 on 2017/12/20.
 */
public class TextProgressBar extends View {
    private final int lineColorBackground;//进度条背景
    private int lineColor;//进度颜色
    private int textColor;//文字颜色
    private int innerBackground;//内圆颜色
    private float lineSize;//进度条宽
    private float textSize;//文字大小
    private int max = 100;
    private int progress = 50;
    private final Paint paint;


    public TextProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextProgressBar);
        textSize = array.getDimension(R.styleable.TextProgressBar_textSize, 20);
        lineSize = array.getDimension(R.styleable.TextProgressBar_lineSize, 20);
        innerBackground = array.getColor(R.styleable.TextProgressBar_innerBackground, Color.WHITE);
        textColor = array.getColor(R.styleable.TextProgressBar_textColor, Color.BLACK);
        lineColor = array.getColor(R.styleable.TextProgressBar_lineColor, Color.RED);
        lineColorBackground = array.getColor(R.styleable.TextProgressBar_lineColorBackground, Color.GREEN);
        max = array.getInt(R.styleable.TextProgressBar_max, 100);
        progress = array.getInt(R.styleable.TextProgressBar_progress, 0);
        array.recycle();
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth() / 2;

        //画背景圆环
        float radius = center - lineSize / 2;
        paint.setColor(lineColorBackground);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(lineSize); // 圆环的宽度
        paint.setAntiAlias(true);//抗锯齿
        canvas.drawCircle(center, center, radius, paint);//空心圆

        //内园
        paint.setColor(innerBackground);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(0); // 圆环的宽度
        canvas.drawCircle(center, center, center - lineSize, paint);//空心圆

        //画圆弧 进度
        RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
        paint.setColor(lineColor);
        paint.setStrokeWidth(lineSize);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);//圆形线冒
        canvas.drawArc(oval, -90, 360 * progress / max, false, paint);


        //画文字
        paint.setColor(textColor);
        paint.setStrokeWidth(0);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.DEFAULT_BOLD);

        int percent = (int) (progress / (float) max * 100);
        String strPercent = percent + "%";
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();//获取线
        float t_x = getWidth() / 2 - paint.measureText(strPercent) / 2;//根据文字宽度计算x
        int t_y = getWidth() / 2 + (fm.bottom - fm.top) / 2 - fm.bottom;//计算文字基线
        canvas.drawText(strPercent, t_x, t_y, paint);
    }

    public void setProgress(int progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("进度Progress不能小于0");
        }
        if (progress > max) {
            progress = max;
        }
        if (progress <= max) {
            this.progress = progress;
            postInvalidate();
        }
    }


}
